<?php
Class Ultility
{
	
    public function replaceurlajax($var,$str,$strr)
    {
        return str_replace($str, $strr, $var);
    }
	
    public function nofollowhref($var)
    {
        return str_replace('href', 'rel="nofollow" href', $var);
    }
	
	/*
	**
		Mã hóa ID
	**
	*/

    public function expNameFile($str)
    {
        $exp = explode('/',$str);
        $count = sizeOf($exp);
        return $exp[$count-1];
    }
    
   private function encode_id($id) {
        $str = dechex($id + 307843210);
        $str = str_replace("1", "i", $str);
        $str = str_replace("2", "w", $str);
        $str = str_replace("3", "o", $str);
        $str = str_replace("4", "u", $str);
        $str = str_replace("5", "z", $str);
        return $str;
    }

    private function decode_id($str) {
        $str = str_replace("i", "1", $str);
        $str = str_replace("w", "2", $str);
        $str = str_replace("o", "3", $str);
        $str = str_replace("u", "4", $str);
        $str = str_replace("z", "5", $str);
        $str = hexdec($str) - 307843210;
        return $str;
    }
	
    public function id_encode($id) {
        $str = dechex($id + 307843210);
        $str = str_replace("1", "i", $str);
        $str = str_replace("2", "w", $str);
        $str = str_replace("3", "o", $str);
        $str = str_replace("4", "u", $str);
        $str = str_replace("5", "z", $str);
        return $str;
    }

    public function id_decode($str) {
        $str = str_replace("i", "1", $str);
        $str = str_replace("w", "2", $str);
        $str = str_replace("o", "3", $str);
        $str = str_replace("u", "4", $str);
        $str = str_replace("z", "5", $str);
        $str = hexdec($str) - 307843210;
        return $str;
    }
    
	public function replace_url($var)
    {
		$vowels = array("-","- "," -","--","---", "/", "+", "=", "{", "}", ")", "(", "!", "`", "~", "@", "#", "$", "%", "^", "&", "*", "_", "!", "!", ".", ",", ";", ":", "]", "[");
		$var = str_replace($vowels, '', $var);
        return $val = str_replace(' ', '-', $var);
    }
	
	public function generateCode($length)
    {
    	$chars = "0123456789ABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789ABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789STUVWXYZ0123456789ABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789";
        $code = "";
        $clen = strlen($chars) - 1;
        while (strlen($code) < $length) {
            $code .= $chars[mt_rand(0,$clen)];
        }
        $salt = $code;
        return $salt;
    }
	
	public function replace_space_to_plus($var)
    {   
        return str_replace(' ','+',$var);
    }
	
	public function printstr($str,$len){
    	$arrstr = explode(" ",$str);
    	$detail = "";
        $return = "";
    	if(count($arrstr) <= $len)
    		$len = count($arrstr);
    	else
    		{
    		  $detail  = " ...";
    		}
    	for($i=0;$i<$len;$i++)
        {
            $return .= $arrstr[$i]." ";
        }
    		
    	return $return.'...';
    }
	
	public function get_ascii($st){
		$vietChar 	= 'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ|é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|ó|ò|ỏ|õ|ọ|ơ|ớ|ờ|ở|ỡ|ợ|ô|ố|ồ|ổ|ỗ|ộ|ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|í|ì|ỉ|ĩ|ị|ý|ỳ|ỷ|ỹ|ỵ|đ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ|Ó|Ò|Ỏ|Õ|Ọ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự|Í|Ì|Ỉ|Ĩ|Ị|Ý|Ỳ|Ỷ|Ỹ|Ỵ|Đ';
		$engChar	= 'a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|e|e|e|e|e|e|e|e|e|e|e|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|u|u|u|u|u|u|u|u|u|u|u|i|i|i|i|i|y|y|y|y|y|d|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|E|E|E|E|E|E|E|E|E|E|E|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|U|U|U|U|U|U|U|U|U|U|U|I|I|I|I|I|Y|Y|Y|Y|Y|D';
		$arrVietChar 	= explode("|", $vietChar);
		$arrEngChar		= explode("|", $engChar);
        $ftc = strtolower(str_replace($arrVietChar, $arrEngChar, $st));
        $vowels = array("-"," -","- "," ", "/", "+", "=", "{", "}", ")", "(", "!", "`", "'", "~", "@", "#", "$", "%", "^", "&", "*", "_", "!", "!", ".", ",", ";", ":", "]", "[","/");
        return str_replace($vowels, ' ', $ftc);
	}
    public function bad_words($str) {
    	$chars = array('địt','Địt','ĐỊT','dit','đéo','Đéo','ĐÉO','lồn','Lồn','LỒN','lon','Buồi','buồi','buồi.','BUỒI','bUồi','buỒi','buồI','buoi','cặc','Cặc','CẶC','dái','Dái','DÁI','Cứt','cứt','CỨT','ỉa','Ỉa','đái','Đái','ỈA','vl','vkl','loz','lin','liz','vll','me');
      	foreach ($chars as $key => $arr)
    		$str = preg_replace( "/(^|\b)".$arr."(\b|!|\?|\.|,|$)/i", "***", $str ); 
    	return $str;
    }
	
	public function getParamGet($param,$type)
	{
		if($type == '1')
        {
            return intval($this->security->xss_clean($_GET[$param]));
        }else{
            return addslashes($this->security->xss_clean($_GET[$param]));
        } 
	}
    public function countCart($arr)
    {
        $i = 0;
        foreach($arr AS $el)
        {
            $i++;
        }
        return $i;
    }
    public function selectType($arr, $id = 0, $name, $start, $attr = '')
    {
        $html = '<select class="select-tag" name="'.$name.'" id="'.$name.'" '.$attr.'>';
        foreach($arr AS $el=>$le)
        {
            if(($el + $start) == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.($el + $start).'" '.$selected.'>'.$le.'</option>';
        }
        $html .= '</select>';
        return $html;
        
    } 
	
	public function status_campaign($active){
		switch ($active){
			case 0 : echo 'Chưa gửi duyệt'; break;
			case 1 : echo '<span style="color: rgb(96, 96, 195);">Đang chờ duyệt</span>'; break;
			case 2 : echo '<span style="color: rgb(65, 195, 45);">Đang chạy</span>'; break;
			case 3 : echo '<span style="color: rgb(244, 30, 30);">Đang tạm dừng</span>'; break;
			case 4 : echo '<span style="color: silver;">Hủy bỏ</span>'; break;
			default : echo 'Không xác định';break; 
		}
	}
    
    public function total_visit($cid,$sid){
        return $this->m_db->where(array('id_campaign' => intval($cid), 'id_shop' => $sid ))->count('log_access');
    }
    
    public function convertDeltatime($dttime){
        $miligiay = $dttime;
        $giay = (($dttime ))%60;
        $phut =  (($dttime - ($giay))/60)%60;
        $gio =  (($dttime - ($phut*60) - ($giay))/60)/60;
        return   $gio.'h, '.$phut."p, ".$giay."s";
    }
    
	public function putOptionSelect($arr,$id = 0)
    {
        $html ='';
        foreach($arr AS $el=>$le)
        {
            if($le['id'] == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le['id'].'" '.$selected.'>'.$le['fullname'].'</option>';
        }
        return $html;
    }

    public function convertTimeRunCampaign($time){
        $time1 = explode(" ",$time);
        $date = $time1[0]; $hours = $time1[1];
        $date1 = explode("-",$date); $date2 = $date1[2].'-'.$date1[1].'-'.$date1[0];
        $hours1 = explode(":",$hours); $hours2 = $hours1[0].':'.$hours1[1];
        return $hours2.' '.$date2;
    }
	
	public function convertNamePlace($id_place){
		$data = $this->db->query('SELECT shopName FROM shop WHERE id = '.$id_place)->row();
		return $data;
	}
	
}